package cn.datax.service.data.market.service.impl;

import cn.datax.common.base.BaseServiceImpl;
import cn.datax.common.exception.DataException;
import cn.datax.service.data.market.cata.dto.CataDto;
import cn.datax.service.data.market.cata.entity.CataEntity;
import cn.datax.service.data.market.cata.entity.CataRelationEntity;
import cn.datax.service.data.market.dao.CataDao;
import cn.datax.service.data.market.dao.CataRelationDao;
import cn.datax.service.data.market.mapstruct.CataMapper;
import cn.datax.service.data.market.service.CataService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuwei
 * @since 2019-09-04
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CataServiceImpl extends BaseServiceImpl<CataDao, CataEntity> implements CataService {
    
    @Autowired
    private CataDao cataDao;
    @Autowired
    private CataRelationDao cataRelationDao;
    @Autowired
    private CataMapper cataMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CataEntity saveCata(CataDto cataDto) {
        CataEntity cata = cataMapper.toEntity(cataDto);
        int n = cataDao.selectCount(Wrappers.<CataEntity>lambdaQuery().eq(CataEntity::getCataName, cata.getCataName()));
        if(n > 0){
            throw new DataException("该部门名已存在");
        }
        cataDao.insert(cata);
        insertBatchRelation(cata);
        return cata;
    }

    private void insertBatchRelation(CataEntity cata) {
        List<CataRelationEntity> relationList = cataRelationDao
                .selectList(Wrappers.<CataRelationEntity>lambdaQuery()
                        .eq(CataRelationEntity::getDescendant, cata.getParentId()))
                .stream().map(relation -> {
                    relation.setDescendant(cata.getId());
                    return relation;
                }).collect(Collectors.toList());
        CataRelationEntity own = new CataRelationEntity();
        own.setDescendant(cata.getId());
        own.setAncestor(cata.getId());
        relationList.add(own);
        if (CollUtil.isNotEmpty(relationList)) {
            cataRelationDao.insertBatch(relationList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CataEntity updateCata(CataDto cataDto) {
        CataEntity cata = cataMapper.toEntity(cataDto);
        cataDao.updateById(cata);
        cataRelationDao.delete(Wrappers.<CataRelationEntity>lambdaQuery()
                .eq(CataRelationEntity::getAncestor, cata.getId()));
        insertBatchRelation(cata);
        return cata;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCataById(String id) {
        int n = cataDao.selectCount(Wrappers.<CataEntity>lambdaQuery().eq(CataEntity::getParentId, id));
        if(n > 0){
            throw new DataException("该部门下存在子部门数据");
        }
        cataDao.deleteById(id);
        cataRelationDao.deleteByAncestor(id);
    }
}
